package com.situ.mall.controller.back;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Suggest;
import com.situ.mall.service.ISuggestService;
import com.situ.mall.service.IUserService;
import com.situ.mall.entity.User;

@Controller 
@RequestMapping("/suggest")
public class SuggestController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ISuggestService suggestService;
	
	@RequestMapping("/getSuggestListPage.action")
	public String getSuggestListPage() {
		return "suggest_list";
	}

	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("SuggestController.selectByPage()");
		Result result = suggestService.selectByPage(page, limit);
		return result;
	}

	@RequestMapping("/cookie.action")
	@ResponseBody
    public Result cookie(HttpServletRequest request, HttpServletResponse response,Model model) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String lastTime=null;
		String last = null;
        Cookie [] cookies = request.getCookies();
        for(int i=0 ;cookies!=null&&i<cookies.length;i++){
            if("time1".equals(cookies[i].getName())){
                lastTime=cookies[i].getValue();
                break;
            }
        }
        if(lastTime==null){
        	last = "您今天第一次访问该网站";
        }else {
        	last = "您上次访问时间为："+lastTime+"";
        }
        String time = new SimpleDateFormat("YYYY-MM-dd-hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("time1",time);
        response.addCookie(cookie);
        return Result.success(last);
    }
	
	@RequestMapping("/replyUser.action")
	public String getUpdateUser(Integer id,Model model) {
		System.out.println("正在进入回复评论界面");
		System.out.println("id = " + id);
		Suggest suggest = suggestService.getReplyUserInfo(id);
		System.out.println(suggest);
		model.addAttribute("suggest", suggest);
		return "suggest_reply";
	}
	
	@RequestMapping("/insertUser.action")
	@ResponseBody
	public Result insertUser(User user) {
		System.out.println(user);
		
		boolean isSuccess = userService.insertUser(user);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("添加用户"+user.getUsername()+"成功");
		}else {
			return Result.error("添加失败");
		}
	}
	
	@RequestMapping("/replyNow.action")
	@ResponseBody
	public Result replyNow(Suggest suggest) {
		System.out.println(suggest);
		boolean isSuccess = suggestService.replyNow(suggest);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("回复用户"+suggest.getUsername()+"成功");
		}else {
			return Result.error("回复失败");
		}
	}
	
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("SuggestController.deleteById()");
		System.out.println("id = " + id);
		
		boolean isSuccess = suggestService.deleteById(id);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("删除评论成功");
		}else {
			return Result.error("删除失败");
		}
	}
	
	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(/* String ids */ Integer[] ids) {
		// System.out.println("ids: " + ids);
		// String[] array = ids.split(",");
		System.out.println(Arrays.toString(ids));// [26, 27]
		boolean isSuccess = userService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
	@RequestMapping("/selectSuggestByUserId")
	@ResponseBody
	public List<Suggest> selectSuggestByUserId(Integer userId) {
		System.out.println("SuggestController.selectSuggestByUserId()");
		System.out.println("UserId = " + userId);
		//查询相关新品
		List<Suggest> listSC = suggestService.selectSuggestByUserId(userId);
		for (Suggest suggest : listSC) {
			System.out.println(suggest);
		}
		return listSC;
	}
	
}
